﻿$(function() {
    initPage();
    bindSubmit();
    formReady();    
});
function initPage() {
    $("#txt_birthday").datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: "1960:2006",
        dateFormat: "yy年mm月dd日"
    });
    $("#txt_region").regions({ dataurl: rootpath + "api/region.ashx", parmsdata: { type: "getCity", pid: 0} });
}
function bindSubmit() {
    $("form").validate({
        rules: {
            txt_account: {
                required: true,
                remote: {
                    url: rootpath + "api/user.ashx",
                    type: "post",
                    data: { type: "checkaccount", account: function() { return $("#txt_account").val(); } }
                }
            },
            txt_realname: { required: true },
            txt_email: {
                email: true,
                remote: {
                    url: rootpath + "api/user.ashx",
                    type: "post",
                    data: { type: "checkemail", email: function() { return $("#txt_email").val(); } }
                }
            },
            txt_password: { required: true, rangelength: [6, 18] },
            txt_repassword: { required: true, equalTo: "#txt_password" },
            txt_region: { required: false },
            txt_school: { required: false }
        },
        messages: {
            txt_account: { required: "* 用户账号不能为空！", remote: "* 该账号已被使用，请重新输入！" },
            txt_realname: { required: "* 真实姓名不能为空！" },
            txt_email: { email: "* 输入的Email格式不正确！", remote: "* 该Email已被使用，请重新输入！" },
            txt_password: { required: "* 密码不能为空！", rangelength: "* 密码长度需在{0}至{1}位之间！" },
            txt_repassword: { required: "* 确认密码不能为空！", equalTo: "* 两次输入的密码不一致！" },
            txt_region: { required: "* 请选择地区！" },
            txt_school: { required: "* 请选择学校！" }
        },
        submitHandler: function() {
            $(".btn").attr("disabled", "disabled").after("<span>提交中.....</span>");
            var usertype = $(":radio[name='usertype']").filter(":checked").val();
            var gender = $(":radio[name='gender']").filter(":checked").val();
            $.ajax({
                type: "POST",
                url: rootpath + "api/user.ashx",
                data: {
                    type: 'adduser',
                    usertype: usertype,
                    account: $("#txt_account").val(),
                    nickname: $("#txt_nickname").val(),
                    realname: $("#txt_realname").val(),
                    email: $("#txt_email").val(),
                    pwd: $("#txt_password").val(),
                    repwd: $("#txt_repassword").val(),
                    schoolid: $("#txt_school").attr("rel"),
                    regionid: $("#txt_regionid").attr("rel"),
                    classid: $("#txt_class").attr("rel"),
                    gender: gender,
                    birthday: $("#txt_birthday").val(),
                    phonenumber: $("#txt_phonenumber").val(),
                    cellphone: $("#txt_cellphone").val(),
                    address: $("#txt_address").val()
                },
                dataType: "string",
                success: function(data) {
                    if (data > 0) {
                        facebox("用户添加成功！");
                        setTimeout("window.location.href='default.aspx';closebox();", 2000);
                    }
                    else {
                        facebox("用户添加失败！");
                    }
                    $(".btn").attr("disabled", "").next("span").remove();
                }
            });
        }
    });
}
